package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.controller.utils.AESUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysStore;
import com.ruoyi.system.service.ISysStoreService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户Controller
 * 
 * @author ruoyi
 * @date 2022-01-17
 */
@Controller
@RequestMapping("/system/store")
public class SysStoreController extends BaseController
{
    private String prefix = "system/store";

    @Autowired
    private ISysStoreService sysStoreService;

    @RequiresPermissions("system:store:view")
    @GetMapping()
    public String store()
    {
        return prefix + "/store";
    }

    /**
     * 查询商户列表
     */
    @RequiresPermissions("system:store:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStore sysStore)
    {
        startPage();
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @RequiresPermissions("system:store:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStore sysStore)
    {
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        ExcelUtil<SysStore> util = new ExcelUtil<SysStore>(SysStore.class);
        return util.exportExcel(list, "商户数据");
    }

    /**
     * 新增商户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商户
     */
    @RequiresPermissions("system:store:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStore sysStore) throws  Exception
    {
        sysStore.setCreateUser(getLoginName());
        String data = sysStore.getCode();
        sysStore.setSecretKey(AESUtil.encrypt(data,AESUtil.APP_SECRET.substring(0,16)));
        if(sysStoreService.checkStoreByCode(data) != null){
            return error("商户编码已经存在："+data);
        }
        return toAjax(sysStoreService.insertSysStore(sysStore));
    }

    /**
     * 修改商户
     */
    @RequiresPermissions("system:store:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysStore sysStore = sysStoreService.selectSysStoreById(id);
        mmap.put("sysStore", sysStore);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户
     */
    @RequiresPermissions("system:store:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStore sysStore)
    {
        SysStore temp = sysStoreService.checkStoreByCode(sysStore.getCode()) ;
        if(temp != null && temp.getId().longValue() != sysStore.getId().longValue()){
            return error("商户编码已经存在："+sysStore.getCode());
        }
        sysStore.setUpdateUser(getLoginName());
        return toAjax(sysStoreService.updateSysStore(sysStore));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("system:store:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStoreService.deleteSysStoreByIds(ids));
    }
}
